<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:msds="http://ehs-info.com/msds" xmlns="http://matsadaml.org/msds/v1.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml"
	exclude-result-prefixes="msds #default">
	<xsl:param name="CompanyDB_v0"
		select="document('v0_ResourceFiles/CompanyDB.xml')/msds:companyDB"/>
	<xsl:param name="RiskPhrases_v0"
		select="document('v0_ResourceFiles/RiskPhrases.xml')/msds:riskPhrases"/>
	<xsl:param name="SafetyPhrases_v0"
		select="document('v0_ResourceFiles/SafetyPhrases.xml')/msds:safetyPhrases"/>
	<xsl:param name="Hazards_v0" select="document('v0_ResourceFiles/Hazards.xml')/msds:hazards"/>
	<xsl:param name="SubstanceDB_v0"
		select="document('v0_ResourceFiles/SubstanceDB.xml')/msds:substanceDB"/>
	<xsl:template match="/">
		<xsl:processing-instruction name="xml-stylesheet">
			<xsl:text>type="text/xsl" href="../../System/Transformations/msds2xhtml.xsl"</xsl:text>
		</xsl:processing-instruction>
		<xsl:apply-templates select="msds:product"/>
	</xsl:template>
	<xsl:template match="msds:product">
		<product id="{@productid}" xmlns="http://matsadaml.org/msds/v1.0"
			xmlns:xhtml="http://www.w3.org/1999/xhtml"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xsi:schemaLocation="http://matsadaml.org/msds/v1.0 ../../System/Schema/msds_v1.0.xsd">
			<xsl:apply-templates select="msds:name"/>
			<xsl:apply-templates select="msds:company"/>
			<xsl:apply-templates select="msds:information"/>
			<xsl:apply-templates select="msds:hazardsIdentification"/>
			<xsl:apply-templates select="msds:substances"/>
			<xsl:apply-templates select="msds:comment"/>
			<xsl:apply-templates select="msds:firstAidMeasures"/>
			<xsl:apply-templates select="msds:fireFightingMeasures"/>
			<xsl:apply-templates select="msds:accidentalRealeaseMeasures"/>
			<xsl:apply-templates select="msds:handlingAndStorage"/>
			<xsl:apply-templates select="msds:personalProtection"/>
			<xsl:apply-templates select="msds:properties"/>
			<xsl:apply-templates select="msds:stability"/>
			<xsl:apply-templates select="msds:toxicology"/>
			<xsl:apply-templates select="msds:ecotoxicology"/>
			<xsl:apply-templates select="msds:disposal"/>
			<xsl:apply-templates select="msds:transportation"/>
			<xsl:apply-templates select="msds:regulatoryInformation"/>
			<xsl:apply-templates select="msds:otherInformation"/>
		</product>
	</xsl:template>
	<xsl:template match="msds:company/@id">
		<xsl:attribute name="key">testcompany.com</xsl:attribute>
	</xsl:template>
	<xsl:template match="msds:substance/@id">
		<xsl:attribute name="db">main-test</xsl:attribute>
		<xsl:attribute name="id">
			<xsl:value-of select="."/>
		</xsl:attribute>
	</xsl:template>
	<xsl:template match="msds:riskPhrase/@id | msds:safetyPhrase/@id | msds:hazardSymbol/@id">
		<xsl:variable name="Code">
			<xsl:choose>
				<xsl:when test="local-name(parent::*) = 'riskPhrase'">
					<xsl:value-of
						select="$RiskPhrases_v0/msds:riskPhrase[@id = current()]/msds:code"/>
				</xsl:when>
				<xsl:when test="local-name(parent::*) = 'safetyPhrase'">
					<xsl:value-of
						select="$SafetyPhrases_v0/msds:safetyPhrase[@id = current()]/msds:code"/>
				</xsl:when>
				<xsl:when test="local-name(parent::*) = 'hazardSymbol'">
					<xsl:value-of select="$Hazards_v0/msds:hazard[@id = current()]/msds:code"/>
				</xsl:when>
			</xsl:choose>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$Code">
				<xsl:attribute name="key">
					<xsl:value-of select="$Code"/>
				</xsl:attribute>
			</xsl:when>
			<xsl:otherwise>
				<xsl:attribute name="key">
					<xsl:value-of select="."/>
				</xsl:attribute>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="msds:paragraph | msds:step">
		<xsl:element name="{local-name()}">
			<xsl:attribute name="countries">
				<xsl:choose>
					<xsl:when test="@lang = 'sv'">SE; </xsl:when>
					<xsl:otherwise>UK; </xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="msds:name[@lang = 'sv']">
		<xsl:element name="{local-name()}">
			<xsl:attribute name="countries">
				<xsl:choose>
					<xsl:when test="@lang = 'sv'">SE; </xsl:when>
					<xsl:otherwise>UK; </xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:apply-templates select="@*"/>
			<xsl:text>Produkt 9999</xsl:text>
		</xsl:element>
	</xsl:template>
	<xsl:template match="msds:name[@lang != 'sv']">
		<xsl:element name="{local-name()}">
			<xsl:attribute name="countries">
				<xsl:choose>
					<xsl:when test="@lang = 'sv'">SE; </xsl:when>
					<xsl:when test="@lang = 'no'">NO; </xsl:when>
					<xsl:otherwise>UK; </xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:apply-templates select="@*"/>
			<xsl:text>Product 9999</xsl:text>
		</xsl:element>
	</xsl:template>
	<xsl:template match="msds:paragraph[not(msds:title/text()) and not(msds:text/text())]"/>
	<xsl:template match="msds:accidentalRealeaseMeasures">
		<xsl:element name="accidentalReleaseMeasures">
			<xsl:apply-templates select="@* | node() | text()"/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="node()">
		<xsl:choose>
			<xsl:when test="local-name()">
				<xsl:element name="{local-name()}">
					<xsl:apply-templates select="@* | node() | text()"/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="@*">
		<xsl:copy/>
	</xsl:template>
	<xsl:template match="xhtml:*">
		<xsl:element name="{name()}">
			<xsl:apply-templates select="@* | node() | text()"/>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>
